package com.kangkang.leetcode10;

import java.util.Arrays;

public class Test1 {
    public static void main(String[] args) {
        int[] a = {1};
        int[] b = {};
        Solution1.merge(a, a.length - b.length, b, b.length);
        System.out.println(Arrays.toString(a));

    }
}

class Solution {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if (m == 0) {
            System.arraycopy(nums2, 0, nums1, 0, nums1.length);
            return;
        }
        if (n == 0) return;

        int x = 0, y = 0;
        int[] o = new int[m + n];
        int index = 0;
        while (index != m + n) {
            if (nums1[x] <= nums2[y]) {
                o[index++] = nums1[x++];
            } else {
                o[index++] = nums2[y++];
            }
            if (x == m) {
                while (y != n) {
                    o[index] = nums2[y++];
                    index++;
                }
            }
            if (y == n) {
                while (x != m) {
                    o[index] = nums1[x++];
                    index++;
                }
            }
        }
        System.arraycopy(o, 0, nums1, 0, nums1.length);
    }
}
class Solution1 {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if (m == 0) {
            System.arraycopy(nums2, 0, nums1, 0, nums1.length);
            return;
        }
        if (n == 0) return;

        System.arraycopy(nums2, 0, nums1, m, nums2.length);
        Arrays.sort(nums1);
    }
}